﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Configuration;
using System.Text.RegularExpressions;

namespace SMSTester.Controls
{
    public partial class DataComparer : UserControl
    {
        public string TableNameFrom = string.Empty;
        public string TableNameTo = string.Empty;
        public string TableNameBodyFrom = string.Empty;
        public string TableNameBodyTo = string.Empty;

        public void Init(bool paramVisible)
        {
            this.Visible = paramVisible;
            if (paramVisible)
            {
                this.Dock = DockStyle.Fill;
            }
        }

        public DataComparer()
        {
            InitializeComponent();
        }

        private DataTable MessageTable = null;
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            using (new CursorWait())
            {
                MessageTable = SQLConnect.Instance.Select(
                    @"
select  
    case when (
		(DIFFERENCE(CAST(body.Body AS nvarchar(max)) , CAST(bodyto.Body AS nvarchar(max)) ) = 4)
		--and
		--(LEN(CAST(body.Body AS nvarchar(max))) = LEN(CAST(bodyto.Body AS nvarchar(max))) )
		and
		(DIFFERENCE(mess.Sender , messto.Sender ) = 4)
		and
		(LEN(mess.Sender) = LEN(messto.Sender ))
		and
		(DIFFERENCE(mess.Recipient , messto.Recipient ) = 4)
		and
		(LEN(mess.Recipient) = LEN(messto.Recipient ))
        ) then 1 else 0 end compare

    ,body.BodyID
	,mess.MessageID
	,body.Body as Body
	,bodyto.Body as BodyTo
	,body.IsUnicode as IsUnicode
	,bodyto.IsUnicode as IsUnicodeTo
	,body.Subject as Subject
	,bodyto.Subject as SubjectTo
	,mess.Sender as Sender
	,messto.Sender as SenderTo
	,mess.Recipient as Recipient
	,messto.Recipient as RecipientTo
    ,messto.ErrorText 
  FROM " + TableNameBodyFrom + @" body
	left join " + TableNameBodyTo + @" bodyto on bodyto.BodyID = body.BodyID
	left join " + TableNameFrom + @" mess on mess.BodyID = body.BodyID
	left join " + TableNameTo + @" messto on messto.MessageID = mess.MessageID
                    ",
                    new List<KeyValuePair<string,Type>>()
                    {
	                    { new KeyValuePair<string,Type>("compare", typeof(int))},
	                    { new KeyValuePair<string,Type>("BodyID", typeof(int))},
	                    { new KeyValuePair<string,Type>("Body", typeof(string))},
	                    { new KeyValuePair<string,Type>("BodyTo", typeof(string))},
	                    { new KeyValuePair<string,Type>("IsUnicode", typeof(bool))},
	                    { new KeyValuePair<string,Type>("IsUnicodeTo", typeof(bool))},
	                    { new KeyValuePair<string,Type>("Subject", typeof(string))},
	                    { new KeyValuePair<string,Type>("SubjectTo", typeof(string))},
	                    { new KeyValuePair<string,Type>("MessageID", typeof(int))},
	                    { new KeyValuePair<string,Type>("Sender", typeof(string))},
	                    { new KeyValuePair<string,Type>("SenderTo", typeof(string))},
	                    { new KeyValuePair<string,Type>("Recipient", typeof(string))},
	                    { new KeyValuePair<string,Type>("RecipientTo", typeof(string))},
	                    { new KeyValuePair<string,Type>("ErrorText", typeof(string))}
                    }
                    );

                bindingSource1.DataSource = MessageTable;

            }

            /*
            select  
                case when (
                    (DIFFERENCE(CAST(body.Body AS nvarchar(max)) , CAST(bodyto.Body AS nvarchar(max)) ) = 4)
                   -- and
                   -- (LEN(CAST(body.Body AS nvarchar(max))) = LEN(CAST(bodyto.Body AS nvarchar(max))) )
                    and
                    (DIFFERENCE(mess.Sender , messto.Sender ) = 4)
                    and
                    (LEN(mess.Sender) = LEN(messto.Sender ))
                    and
                    (DIFFERENCE(mess.Recipient , messto.Recipient ) = 4)
                    and
                    (LEN(mess.Recipient) = LEN(messto.Recipient ))
                    ) then 1 else 0 end compare
                ,body.BodyID
                ,mess.MessageID
                ,body.Body as Body
                ,bodyto.Body as BodyTo
                ,body.IsUnicode as IsUnicode
                ,bodyto.IsUnicode as IsUnicodeTo
                ,body.Subject as Subject
                ,bodyto.Subject as SubjectTo
                ,mess.Sender as Sender
                ,messto.Sender as SenderTo
                ,mess.Recipient as Recipient
                ,messto.Recipient as RecipientTo
                ,messto.ErrorText 
              FROM Sky_SMSTestStorageMessagesToSMTPBody body
                left join Sky_SMSTestStorageMessagesFromSMTPToGatewayBody bodyto on bodyto.BodyID = body.BodyID
                left join Sky_SMSTestStorageMessagesToSMTP mess on mess.BodyID = body.BodyID
                left join Sky_SMSTestStorageMessagesFromSMTPToGateway messto on messto.MessageID = mess.MessageID             
             
                         */

        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            using (new CursorWait())
            {
                SQLConnect.Instance.ExecuteNonQuery("delete from " + TableNameTo);
                SQLConnect.Instance.ExecuteNonQuery("delete from " + TableNameBodyTo);
                SQLConnect.Instance.ExecuteNonQuery(@"
INSERT INTO [" + TableNameBodyTo + @"]
           ([BodyID]
           ,[Body]
           ,[IsUnicode]
           ,[Subject])
SELECT [BodyID]
      ,[Body]
      ,[IsUnicode]
      ,[Subject]
  FROM [" + TableNameBodyFrom + @"]
");
                SQLConnect.Instance.ExecuteNonQuery(@"
INSERT INTO [" + TableNameTo + @"]
           ([MessageID]
           ,[Sender]
           ,[Recipient]
           ,[BodyID])
SELECT [MessageID]
      ,[Sender]
      ,[Recipient]
      ,[BodyID]
  FROM [" + TableNameFrom + @"]
");
                btnRefresh_Click(btnRefresh, new EventArgs());
            }
        }

    }
}
